(function($){
    var account = new Object();

    /*---------------------------------------------------------------------*/
    account.refresh = function(callback){
        $("#chute").load(ROOT + "/logined/account/list.do", function(){
			/* event button */
			$("#chute :button:first").bind("click",function(){
				/* cancel the highlight */
				$("#chute .highlight").removeClass("highlight");
				/* clear arena */
				var arena = $("#arena");
				arena.empty();
				arena.load(ROOT + "/logined/account/edit.do",function(){
					var btns = $("#arena :button");
					/* for save */
					$(btns[0]).one("click",saveAccount);
					/* for delete */
					$(btns[1]).hide();
				});
			});
			/* event <a> */
            $("#chute a").each(function(index,ele){
				$(ele).bind("click",function(){
					/* cancel the highlight */
					$("#chute .highlight").removeClass("highlight");
					/* hight current */
					$(this).addClass("highlight");
					/* load */
					var name = this.innerHTML;
					var arena = $("#arena");
					arena.empty();
					arena.load(ROOT + "/logined/account/edit.do",{
						name: name
					},renderAccount);
				});
			});
			if(typeof callback == "function")
				callback();
        });
    };
	/*---------------------------------------------------------------------*/
	function saveAccount(){
		var arena = $("#arena");
		var d = new Object();
		arena.find(":text,:password,select").each(function(index,ele){
			d[ele.name] = ele.value;
		});
		arena.load(ROOT + "/logined/account/save.do",d,function(){
			renderAccount();
			/* highligh chute */
			account.refresh(function(){
				$("#chute a").each(function(index,ele){
					if($(ele).text() == d.name)
						$(ele).addClass("highlight");
					else
						$(ele).removeClass("highlight");
				});
			});
		});
	}
	/*---------------------------------------------------------------------*/
	function renderAccount(){
		var btns = $("#arena :button");
		/* for save */
		$(btns[0]).one("click",saveAccount);
		/* for delete */
		$(btns[1]).one("click",function(){
			var name = $("#arena :text[name=name]").val();
			$("#arena").load(ROOT + "/logined/account/del.do",{name:name},function(){
				account.refresh();
			});
		});
	}
    /*---------------------------------------------------------------------*/
    // Install to jQuery
    $.account = account;
})(jQuery);
